{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Simulating Probabilities [demonstration]\n",
    "The code below shows one method for simulating dice rolls. Read through it and try to understand how it works. \n",
    "\n",
    "**What does the data stored in `roll_counts` represent?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import the random module and reference it as rd\n",
    "import random as rd\n",
    "\n",
    "\n",
    "def simulate_dice_rolls(N):\n",
    "    \"\"\"\n",
    "    Simulates dice rolls\n",
    "    \n",
    "    Args:\n",
    "        N (int): The number of trials\n",
    "        \n",
    "    Returns:\n",
    "        list: roll counts [1,6]\n",
    "    \"\"\"\n",
    "    # Create a list to track the 6 options for the roll\n",
    "    roll_counts = [0,0,0,0,0,0]\n",
    "    for i in range(N):\n",
    "        # Randomly select a value from the list (1 to 6)\n",
    "        roll = rd.choice([1,2,3,4,5,6]) \n",
    "        # Recall indices start at 0 so we need to decrement\n",
    "        index = roll - 1\n",
    "        roll_counts[index] = roll_counts[index] + 1\n",
    "    return roll_counts\n",
    "\n",
    "def show_roll_data(roll_counts):\n",
    "    \"\"\"\n",
    "    Shows the dice roll data\n",
    "    \n",
    "    Args:\n",
    "        roll_counts (list): The roll counts stored in the list\n",
    "        \n",
    "    Returns:\n",
    "        list: roll counts [1,6]\n",
    "    \"\"\" \n",
    "    # Gets the number of sides of the dice and prints\n",
    "    # the side of the die. \n",
    "    # enumerate creates the position of the die and the\n",
    "    # list value\n",
    "    for dice_side, frequency in enumerate(roll_counts):\n",
    "        print(dice_side + 1, \"was rolled\", frequency, \"times\")\n",
    "        \n",
    "roll_data = simulate_dice_rolls(1000)\n",
    "show_roll_data(roll_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Basic Data Visualization [optional]\n",
    "This section is optional but you may find it interesting.\n",
    "\n",
    "You'll learn more about this throughout the Nanodegree, but  now is a great time to look at one data visualization tool called a histogram."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Matplotlib is one of the most common plotting packages in Python\n",
    "# to use it more succinctly, you can call it   \n",
    "from matplotlib import pyplot as plt\n",
    "# This line is needed\n",
    "%matplotlib inline\n",
    "\n",
    "def visualize_one_die(roll_data):\n",
    "    \"\"\"\n",
    "    Visualizes the dice rolls\n",
    "    \n",
    "    Args:\n",
    "        roll_data (int): roll counts in a list from [1,6]\n",
    "        \n",
    "    Returns:\n",
    "        None - shows a plot with the x-axis is the dice values\n",
    "               and the y-axis as the frequency for t\n",
    "    \"\"\"\n",
    "    roll_outcomes = [1,2,3,4,5,6]\n",
    "    fig, ax = plt.subplots()\n",
    "    ax.bar(roll_outcomes, roll_data)\n",
    "    ax.set_xlabel(\"Value on Die\")\n",
    "    ax.set_ylabel(\"# rolls\")\n",
    "    ax.set_title(\"Simulated Counts of Rolls\")\n",
    "    plt.show()\n",
    "    \n",
    "roll_data = simulate_dice_rolls(500)\n",
    "visualize_one_die(roll_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
